package rocksea.com.wechat

import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * Created by Mrw on 2018/2/4.
 *
 */
class RetrofitServiceManager private constructor()
{
    var mRetrofit:Retrofit
    init
    {
        val builder:OkHttpClient.Builder = OkHttpClient.Builder()
        builder.connectTimeout(DEFAULT_TIME_OUT.toLong(),TimeUnit.SECONDS)
        builder.readTimeout(DEFAULT_READ_TIME.toLong(),TimeUnit.SECONDS)
        val mRequestInterceptor = RequestInterceptor()
        val mHttpCommonInterceptor = HttpCommonInterceptor()
        builder.addInterceptor(mHttpCommonInterceptor)
        mRetrofit = Retrofit.Builder()
                .client(builder.build())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("")
                .build()
    }
    companion object
    {
        private const val DEFAULT_TIME_OUT = 5
        private const val DEFAULT_READ_TIME = 10
        val instance:RetrofitServiceManager by lazy {
            RetrofitServiceManager()
        }
    }

    fun <T>getService(service:Class<T> )
    {
        mRetrofit.create(service)
    }

}